//
//  CompanyCell.m
//  HuaRun
//
//  Created by admin on 17/8/28.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "CompanyCell.h"
#import "CompanyModel.h"
#import "LineView.h"

@interface CompanyCell ()
@property (nonatomic, strong) UIButton * firstCharBtn;
//名字
@property (nonatomic, strong) UILabel * nameLab;
//公司
@property (nonatomic, strong) UILabel * companyLab;
//分割线
@property (nonatomic, strong) LineView * lineView;

@end

@implementation CompanyCell

#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    [super viewInit];
    
    [self.contentView addSubview:self.firstCharBtn];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.companyLab];
    [self.contentView addSubview:self.lineView];

}

- (void)layoutInit {
    
    weakself(ws);
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.contentView).offset(kMargin10);
        make.right.mas_equalTo(ws.contentView).offset(-kMargin10);
        make.top.mas_equalTo(ws.contentView).offset(0);
        make.bottom.mas_equalTo(ws.contentView).offset(0);
    }];
    
    [self.firstCharBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
        make.top.mas_equalTo(ws.nameLab).offset(kMargin10);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.firstCharBtn.mas_right).offset(kMargin10);
        make.top.mas_equalTo(ws.backView).offset(kMargin10);
        make.right.mas_equalTo(ws.backView).offset(-kMargin10);
        make.height.mas_equalTo(20);
    }];
    
    [self.companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.nameLab.mas_bottom).offset(kMargin10/2);
        make.right.mas_equalTo(ws.nameLab);
        make.height.mas_equalTo(ws.nameLab);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView);
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
        make.right.mas_equalTo(ws.backView).offset(0);
        make.height.mas_equalTo(1);
    }];

//    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.mas_equalTo(ws);
//        make.bottom.mas_equalTo(ws.companyLab).offset(kMargin10);
//    }];
}

- (void)setCompanyModel:(CompanyModel *)companyModel {
    _companyModel = companyModel;
    _nameLab.text = [NSString stringWithFormat:@"%@", companyModel.name];
    _companyLab.text = [NSString stringWithFormat:@"%@", companyModel.companyNameChineseC];
    if (companyModel.name.length > 1) {
        NSString * firstStr = [[companyModel.name substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
    }

}

-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

#pragma mark - getter

- (UIButton *)firstCharBtn {
    if (!_firstCharBtn) {
        self.firstCharBtn = [UIButton new];
        _firstCharBtn.titleLabel.font = kFontSize10;
        _firstCharBtn.titleLabel.textColor = [UIColor whiteColor];
        
        int n = [self getRandomNumber:1 to:8];
        NSString * str = [NSString stringWithFormat:@"0%d", n];
        [_firstCharBtn setBackgroundImage:[UIImage imageNamed:str]
                                 forState:(UIControlStateNormal)];
    }
    return _firstCharBtn;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = kGeneralFontSize;
        _nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    }
    return _nameLab;
}

- (UILabel *)companyLab {
    if (!_companyLab) {
        self.companyLab = [UILabel new];
        _companyLab.textAlignment = NSTextAlignmentLeft;
        _companyLab.font = kGeneralFontSize;
        _companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    }
    return _companyLab;
}

- (LineView *)lineView {
    if (!_lineView) {
        self.lineView =  [LineView new];
        
    }
    return _lineView;
}
@end
